package com.ccrfid.rmc6.resource.poi;

import com.ccrfid.rmc6.appconfig.exception.pojo.AppException;
import com.ccrfid.rmc6.appconfig.exception.pojo.CustomErrors;
import com.ccrfid.rmc6.entity.poi.PoiLabelEntity;
import com.ccrfid.rmc6.pojo.param.SortParam;
import com.ccrfid.rmc6.repository.search.Criterion;
import com.ccrfid.rmc6.repository.search.SearchCriteria;
import com.ccrfid.rmc6.service.poi.PoiLabelService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.ArrayList;
import java.util.List;

@Path("labels")
@Produces(MediaType.APPLICATION_JSON)
public class PoiLabelResource {
    private static final Logger logger = LoggerFactory.getLogger(PoiLabelResource.class);
    @Autowired
    private PoiLabelService poiLabelService;

    @GET
    public List<PoiLabelEntity> getPoiLabelList(@QueryParam("labelText") String labelText,
                                                @QueryParam("mapLevel") Integer mapLevel,
                                                @QueryParam("floorNumber") Integer floorNumber,
                                                @BeanParam SortParam sortParam) {
        SearchCriteria criteria = SearchCriteria.Builder.create();
        if (labelText != null) {
            criteria.add(new Criterion("labelText", Criterion.Operator.LIKE, '%' + labelText + '%'));
        }
        if (mapLevel != null) {
            criteria.add(new Criterion("mapLevel", Criterion.Operator.EQ, mapLevel));
        }
        if (floorNumber != null) {
            criteria.add(new Criterion("floorNumber", Criterion.Operator.EQ, floorNumber));
        }
        List<PoiLabelEntity> list;
        if (sortParam.getOrders() != null && sortParam.getOrders().size() > 0) {
            list = poiLabelService.getPoiLabelList(criteria, Sort.by(sortParam.getOrders()));
        } else {
            list = poiLabelService.getPoiLabelList(criteria, Sort.by(new ArrayList<>()));
        }
        return list;
    }

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    public PoiLabelEntity createPoiLabel(PoiLabelEntity entity) {
        PoiLabelEntity poiLabelEntity = poiLabelService.createPoiLabel(entity);
        return poiLabelEntity;
    }

    @DELETE
    @Consumes(MediaType.APPLICATION_JSON)
    public int deletePoiLabels(List<Long> idList) throws AppException {
        if (idList == null) {
            throw new AppException(Response.Status.BAD_REQUEST.getStatusCode(),
                    CustomErrors.param_is_null.getCode(), CustomErrors.param_is_null.getReason(), "");
        }
        return poiLabelService.deletePoiLabels(idList);
    }

    @GET
    @Path("{id}")
    public PoiLabelEntity getPoiLabelById(@PathParam("id") Long labelId) {
        PoiLabelEntity poiLabelEntity = poiLabelService.getPoiLabelById(labelId);
        return poiLabelEntity;
    }

    @PUT
    @Path("{id}")
    @Consumes(MediaType.APPLICATION_JSON)
    public PoiLabelEntity updatePoiLabel(@PathParam("id") Long labelId, PoiLabelEntity entity) throws AppException {
        PoiLabelEntity poiLabelEntity = poiLabelService.updatePoiLabel(labelId, entity);
        return poiLabelEntity;
    }
}
